var modifyOrAddFlag;
var checkResultData = [ {
	valueCode : '0',
	valueName : '不可用'
}, {
	valueCode : '1',
	valueName : '可用'
} ];
$(document).ready(function(){
	
		// 存放选中的调度的领域接口的URL
		window.systemInterfaceUrl = null;
		// 存放选中的调度的运行状态
		window.runStatus = null;
	
		$('#win').window('close');
		//所属领域下拉框
		$('#comboxSearchfieldName').combobox({
			url: BASE_URL_FIELD_SEARCH + '/findAllOfCurrentUserCanManage' + '?v=' + Math.random(),
			valueField: 'id',
		    textField: 'name',
		    editable: false,
		    method:'get',
		    loadFilter: function(data){
		    	var allCheck = {'id':'','name':'全部'};
		    	data.content.unshift(allCheck);
		    	return data.content;
		    },
		    onSelect: function(record){
		    	comboxSearchSystemName.combobox({
                    disabled: false,
                    method:'get',
                    queryParams: {fieldId: record.id},
                    url: BASE_URL_FIELD_SYSTEM_SEARCH +'/findByFieldOfCurrentUserCanManage' + '?v=' + Math.random(),
                    valueField: 'id',
                    textField: 'name',
					data:'',
        		    loadFilter: function(data){
        		    	var allCheck = {'id':'','name':'全部'};
        		    	data.content.unshift(allCheck);
        		    	return data.content;
        		    },
        		    onLoadSuccess:function(){
        		    	comboxSearchSystemName.combobox('setValue','');
        		    }
                }).load();
		    },
		    onLoadSuccess:function(){
		    	$('#comboxSearchfieldName').combobox('setValue','');
		    }
		});
		
		//所属系统下拉框
		var comboxSearchSystemName = $('#comboxSearchSystemName').combobox({
            disabled: true,
            valueField: 'id',
            textField: 'name',
            editable: false,
		  data:[{id:'',name:'全部','selected':'true'}]
        });
		
		//接口状态下拉框
		$('#comboxSearchInterfaceStatus').combobox({
			valueField: 'valueCode',
		    textField: 'valueName',
		    editable:false,
		    method:'get',
		    data : checkResultData,
		    loadFilter: function(data){
		    	var allCheck = {'valueCode':'','valueName':'全部'};
    		    data.unshift(allCheck);
    		    return data;
		    }
		});
		
		$('#comboxFieldName').combobox({
			url: BASE_URL_FIELD_SEARCH + '/findAllOfCurrentUserCanManage' + '?v=' + Math.random(),
			valueField: 'id',
		    textField: 'name',
		    editable:false,
		    method:'get',
		    loadFilter: function(data){
		    	var allCheck = {'id':'','name':'请选择'};
		    	if(data.content)
		    	{
		    		data.content.unshift(allCheck);
		    	}
		    	else
		    	{
		    		return data;
		    	}
		    	return data.content;
		    },
		    onSelect: function(record){
		    	comboxSystemName.combobox({
                    disabled: false,
                    method:'get',
                    queryParams: {fieldId: record.id},
                    url: BASE_URL_FIELD_SYSTEM_SEARCH +'/findByFieldOfCurrentUserCanManage' + '?v=' + Math.random(),
                    valueField: 'id',
                    textField: 'name',
					data:'',
        		    loadFilter: function(data){
        		    	
        		    	var allCheck = {'id':'','name':'请选择'};
        		    	data.content.unshift(allCheck);
        		    	return data.content;
        		    },
        		    onSelect: function(record){
        		    	comboxInterface.combobox({
                            disabled: false,
                            method:'get',
                            queryParams: {fieldSystemId: record.id},
                            url: BASE_URL_SYSTEM_INTERFACE_SEARCH +'/findByOwnerFieldSystem_Id' + '?v=' + Math.random(),
                            valueField: 'id',
                            textField: 'jobName',
        					data:'',
                		    loadFilter: function(data){
                		    	if(data[0].id == ""){
                		    		return data;
                		    	}
                		    	var allCheck = {'id':'','jobName':'请选择'};
	                		    data.unshift(allCheck);
	                		    return data;
                		    	
                		    },
                		    onLoadSuccess:function(){
                		    	$('#comboxInterface').combobox('setValue','');
                		    }
                        });
        		    },
        		    onLoadSuccess:function(){
        		    	$('#comboxInterface').combobox('setValue','');
        		    	
        		    }
                }).load();
		    	comboxInterface.combobox({
		    		disabled: true,
		            valueField: 'id',
		            textField: 'jobName',
		            editable: false,
		            url:'',
					data:[],
					loadFilter:function(data){
						var allCheck = {id:'',jobName:'请选择','selected':'true'};
						data.unshift(allCheck);
						return data;
					}
                });
		    }
		    
		});
		
		
		var comboxSystemName = $('#comboxSystemName').combobox({
            disabled: true,
            valueField: 'id',
            textField: 'name',
            editable:false,
			url:'',
			data:[],
			loadFilter:function(data){
				var allCheck = {id:'',name:'请选择','selected':'true'};
				data.unshift(allCheck);
				return data;
			}
		  	
        });
		
		//所属系统下的所有接口
		var comboxInterface = $('#comboxInterface').combobox({
			disabled: true,
            valueField: 'id',
            textField: 'jobName',
            editable: false,
            url:'',
			data:[],
			loadFilter:function(data){
				var allCheck = {id:'',jobName:'请选择','selected':'true'};
				data.unshift(allCheck);
				return data;
			}
		})
		
		$('#comboxExecutionCycleTime').combobox({
			url: BASE_URL_DATA_DICTIONARY_VALUE_SEARCH +'/findByTypeCode?typeCode='+DDTC_KETTLE_JOB_SCHEDULE_CYCLE_UNIT + '&v=' + Math.random(),
			valueField: 'valueCode',
		    textField: 'valueName',
		    editable:false,
		    method:'get',
		    loadFilter:function(data){
		    	return data;
		    },
		    onChange:function(e){
		    	var chooseVal = $("#comboxExecutionCycleTime").combobox('getValue');
				var cycleNum = $('#txtExecutionCycleNumber').val();
				if(chooseVal == "5" && cycleNum == "1")
				{
					$(".daySel, .txtEvery, .text_tips, #end_time_li").show();
				}else if(chooseVal == "1")
				{
					$(".txtEvery, .daySel, .text_tips, #end_time_li").hide();
				}else
				{
					$(".txtEvery, .text_tips, #end_time_li").show();
					$(".daySel").hide();
				}
		    }
		    
		});
		
		$("#datagrid").datagrid({
			url :BASE_URL_API
			+ '/serviceUsability/getServiceUsabilityTestResult'
			+ '?v=' + Math.random(),
			method:'get',
			autoRowHeight:true,
			singleSelect:true,
			striped:true,
			nowrap:false,
			pageSize:20,
			pageList:[10,20,50,100],
			columns:[[
						{field:'fieldName',title:'领域',width:80},
						{field:'systemName',title:'系统',width:70},
						{field:'type',title:'接口类型',width:100,align:'left', formatter: function(value, rowData, rowIndex){
							return value == '1'?"web service接口":"REST接口";
						}},
						{field:'name',title:'接口名称',width:115,align:'left'},
						{field:'createTime',title:'接口注册时间',width:100},
						{field:'version',title:'接口版本',width:50},
						{field:'status',title:'检测结果',width:50,formatter: function(value, rowData, rowIndex){
							return value == '1'?'<img src="../../../images/icons/yes.png" height="24" width="24"/>':'<img src="../../../images/icons/no.png" height="24" width="24"/>';
						}},
						{field:'desc',title:'接口描述',width:150},
						{field:'remark',title:'备注',width:150,align:'left'}
			      ]],
			pagination:true,
			onSelect:function(rowIndex,rowData){
				if(rowData.runStatusForDisplay == "暂停")
				{
					$(".startOrStop .l-btn-text").text("重启");
					$(".startOrStop .icon-more").css("background","url(../../images/icons/restart.png) no-repeat center center"); 
				}else if(rowData.runStatusForDisplay == "正常")
				{
					$(".startOrStop .l-btn-text").text("暂停");
					$(".startOrStop .icon-more").css("background","url(../../images/icons/stop.png) no-repeat center center"); 
				}
				$("#toolbar_edit").linkbutton('enable');
				$("#toolbar_delete").linkbutton('enable');
			},
			loadFilter:function(data){
				//alert("inData:"+JSON.stringify(data));
				return {
					total: data.totalElements,
					rows: data.content
				};
			},
			onBeforeLoad:function(param){
				param.page -= 1;
				param.size = param.rows;
				delete param.rows;
				
				param.sort = 'createTime,desc';
				param.auditStatus='1';
				var fieldId = $('#comboxSearchfieldName').combobox('getValue');
				if(fieldId !=""){
					param.fieldId = fieldId;
				}
				
				var fieldSystemId = $('#comboxSearchSystemName').combobox('getValue');
				if(fieldSystemId !=""){
					param.fieldSystemId = fieldSystemId;
				}
				
				var status = $('#comboxSearchInterfaceStatus').combobox('getValue');
				if(status !=""){
					param.status = status;
				}
				 
				var name = $('#txtSearchInterfaceName').val();
				if(name !=""){
					param.name = name;
				}
			}
		});
		
		$("#datagrid").datagrid('getPager').pagination({   
		    displayMsg:'当前显示从 [{from}] 到 [{to}] 共[{total}]条记录'
		}); 
	$(".panel-tool-close, #instance_cancel").click(function(e) {
		$("body").css("overflow-x","inherit")
	});

});

	//查询
	function search(){
		$('#datagrid').datagrid('load');
	}
	function refresh(){
		$('#datagrid').datagrid('load');
	}
	

	